<?php

namespace app\client\controller;

use app\common\Auth;
use app\common\model\Commond;
use app\common\model\Device;
use app\common\model\Msg;
use think\Controller;
use think\Log;

class Xfyun extends Controller
{
    private  function get_cmd($str,$cmds){
        if(is_array($str)){
            $str = $str[0];
        }
        $items = str_split($str);
        $pow = [-1=>0];
        foreach($cmds as $k=>$cmd){
            $pow[$k] = 0;
            foreach($items as $item){
                if(strpos($cmd->keyword,$item)!==false){
                    $pow[$k]++;
                }
            }
        }
        if(is_array($pow)){
            $key = array_search(max($pow),$pow);
            return  (isset($cmds[$key]) && $cmds[$key] !== 0)?$cmds[$key]:[];
        }
        return [];
    }
    public function speech(){

    }
    function index(){
        $user = Auth::getUser();
        if(empty($user)){
            echo "<script> location.href='/login?r=/client' </script>";
        }
        $user_id = Auth::getUserId();
        $list = Commond::userCommond($user_id);
        $this->assign('list',$list);
        return view();
    }
    function sendCmd($id = 0){
        $cmd = Commond::get($id);
        if(isset($cmd->device_id)){
            $devid = $cmd->device_id;
            $user_id = Auth::getUserId();
            $device_id = Device::userDeviceId($user_id);
            if(is_array($device_id)){
                if(in_array($devid,$device_id)){
                    Msg::send($cmd);//发送指令
                    return json([
                        "code"=>1000,
                        "msg"=>"发送成功"
                    ]);
                }
            }
        }
        return json([
            "code"=>1010,
            "msg"=>"失败"
        ]);
    }
}
